//
//  RadioThirdViewController.m
//  A_program
//
//  Created by BayMax on 15/5/23.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "RadioThirdViewController.h"
#import "RadioSecondTableViewCell.h"
#import <AFNetworking/AFNetworking.h>
#import "ThirdListModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "RadioView.h"
#import "PlayerViewController.h"
#import <MJRefresh.h>
@interface RadioThirdViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *headArray;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, retain) NSDictionary *dict;

@end

@implementation RadioThirdViewController

- (void)dealloc
{
    [_tableArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)createWeb
{
    NSNumberFormatter *num = [[NSNumberFormatter alloc]init];
    NSString *url = [num stringFromNumber:self.myId];
    NSString *urlPath = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/30?device=iPhone", url];
    
    //将网址里面的汉字转换成字符串
    urlPath = [urlPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:urlPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        self.dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSMutableArray *smallArray = [NSMutableArray array];
        NSMutableDictionary *dic = [self.dict objectForKey:@"tracks"];
        self.tableArray = [dic objectForKey:@"list"];
        for (NSMutableDictionary *dic in _tableArray) {
            ThirdListModel *model = [[ThirdListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [smallArray addObject:model];
        }
        self.array = [NSMutableArray arrayWithArray:smallArray];
        [self createView];
        [self.table reloadData];
    }
     
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"下载错误 is %@", error);
         }];
}

- (void)createView
{
    NSMutableDictionary *headdic = [self.dict objectForKey:@"album"];
    RadioView *view = [[RadioView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 160)];
    view.nameLabel.text = [headdic objectForKey:@"nickname"];
    
    NSString *smallStr = [headdic objectForKey:@"coverSmall"];
    [view.smallImageView sd_setImageWithURL:[NSURL URLWithString:smallStr] placeholderImage:nil];
    view.smallImageView.layer.cornerRadius = 25;
    view.smallImageView.layer.masksToBounds = YES;
    
    NSString *likeStr = [NSString stringWithFormat:@"%@", [headdic objectForKey:@"tracks"]];
    view.likeLabel.text = likeStr;
    
    NSString *bigStr = [headdic objectForKey:@"coverLarge"];
    [view.bigImageView sd_setImageWithURL:[NSURL URLWithString:bigStr] placeholderImage:nil];
    
    NSString *superStr = [headdic objectForKey:@"coverOrigin"];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:superStr] placeholderImage:nil];
    imageView.alpha = 0.2;
    [view addSubview:imageView];
    self.table.tableHeaderView = view;
    
    [self.table reloadData];
}


- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 69) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table release];
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    RadioSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[RadioSecondTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    
    ThirdListModel *model = [self.array objectAtIndex:indexPath.row];
    cell.secondLabelName.text = model.title;
    cell.secondLabelAuthor.text = model.nickname;
    NSString *playtimes = [NSString stringWithFormat:@"%@", model.playtimes];
    cell.secondLabelCount.text = playtimes;
    NSString *like = [NSString stringWithFormat:@"%@", model.likes];
    cell.secondLabelZan.text = like;
    
    [cell.secondImageView sd_setImageWithURL:[NSURL URLWithString:model.coverSmall] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.secondImageView.layer.cornerRadius = 30;
    cell.secondImageView.layer.masksToBounds = YES;
    
    
    
    return cell;
    
    
    return nil;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

// tableView 跳转到播放界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *play = [[PlayerViewController alloc]init];
    ThirdListModel *model = [self.array objectAtIndex:indexPath.row];
    play.trackId = model.trackId;
    play.array = self.tableArray;
    [self presentViewController:play animated:YES completion:nil];
    [play release];
    
}



- (void)loadView
{
    [super loadView];
    [self createTableView];
    [self createWeb];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}



@end
